import $ from '../common';
Page({
  data: {
    titleHeight: $.gData().titleHeight,
    statuHeight: $.gData().statuHeight,
    isIphoneX: $.gData().isIphoneX,
    growing: {},
    group: {},
    textvalue:'',
    imglist:[],
    _components_modal_hide: true
  },
  // 重新发送
  againrelease() {
    this.setData({
      _components_modal_hide: false
    })
  },
  parent() {
    $.parent().setData({
      page: 1,
      info: []
    })
    $.parent().getinfo()
  },
  choosimg(e){//放大图片
    const {src}=e.currentTarget.dataset
    const {imglist} = this.data
    wx.previewImage({
      current: src, // 当前显示图片的http链接
      urls: imglist // 需要预览的图片http链接列表
    })
  },
  //发布
  release() {
    const {textvalue,imglist,growing_uuid} = this.data
    if (!textvalue) {
      $.tip('评价内容不能为空')
      return
    }
    $.post('/group/growing/publish', {
      content: textvalue||'',
      imgs: imglist||[],
      growing_uuid: growing_uuid
    }, res => {
      this.parent()
      wx.navigateBack({
        delta: -1,
      })
    })
  },
  //删除
  delete() {
    const {growing_uuid} = this.data
    $.get('/group/growing/remove', {
      growing_uuid
    }, res => {
      this.parent()
      wx.navigateBack({
        delta: -1,
      })
    })
  },
  //保存草稿
  savedrafts() {
    const {textvalue,imglist,growing_uuid} = this.data
    // if(!textvalue){
    //   $.tip('评价内容不能为空')
    //   return
    // }
    $.post('/group/growing/update', {
      content: textvalue||'',
      imgs:imglist||[],
      growing_uuid: growing_uuid
    }, res => {
      this.parent()

      wx.navigateBack({
        delta: -1,
      })
    })
  },
  cancel() { //取消发布
    this.setData({
      _components_modal_hide: true
    })
  },
  determine(e) { //确定
    this.cancel()
    this.release()
  },
  getinfo() { //获取用户信息
    const { group_uuid,growing_uuid} = this.data
    let {textvalue,imglist}=this.data
    $.get('/group/growing/detail', {
      group_uuid: group_uuid,
      growing_uuid: growing_uuid,
    }, res => {
      if(!textvalue){
        textvalue=res.growing.content ||''
        imglist=res.growing.imgs||[]
      }
      this.setData({
        group: res.group,
        growing: res.growing,
        textvalue,
        imglist
      })
    })
  },
  goestreview() { //去往编辑
    $.navigateTo(`/Signin/estreview`)
  },
  gostudentreview(e) { //去往学员点评
    const {
      student_uuid,
      growingstudent_uuid
    } = e.currentTarget.dataset
    $.navigateTo(`/Signin/stureview?student_uuid=${student_uuid}&campus_uuid=${this.data.campus_uuid}&growingstudent_uuid=${growingstudent_uuid}&growing_uuid=${this.data.growing_uuid}`)
  },
  creatinfo(){//创建
    const {group_uuid,textvalue,imglist}=this.data
    $.post('/group/growing/create',{
      content:textvalue|| '',
      imgs:imglist|| [],
      title: '',
      group_uuid
    },res=>{
      this.setData({
        growing_uuid:res.growing_uuid
      })
      this.getinfo()
    })
  },
  onLoad(e) {
    const {
      group_uuid = '', growing_uuid = '', campus_uuid = '', iscreate = ''
    } = e
    this.setData({
      group_uuid,
      campus_uuid,
      growing_uuid,
      iscreate
    })
  },
  onShow() {
    if (this.data.iscreate) {
      this.creatinfo()
    } else {
      this.getinfo()
    }
  },
})